<template>
  <div>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="题干">
        <el-input
          type="textarea"
          autosize
          placeholder="请输入内容"
          v-model="form.stem"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="试题类型">
        <el-select
          v-model="form.questionType.id"
          placeholder="请选择"
          @change="chooseQuestionType()"
        >
          <el-option
            v-for="item in questionTypeList"
            :key="item.id"
            :label="item.qtname"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="难度">
        <el-select v-model="form.difficulty.id" placeholder="请选择">
          <el-option
            v-for="item in difficultyList"
            :key="item.id"
            :label="item.dname"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学科">
        <el-select
          v-model="form.subject.id"
          placeholder="请选择"
          @change="chooseSubject()"
        >
          <el-option
            v-for="item in subjectList"
            :key="item.id"
            :label="item.sname"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="阶段">
        <el-select
          v-model="form.stage.id"
          placeholder="请选择"
          :disabled="isDisabled"
          title="请先选择学科"
        >
          <el-option
            v-for="item in stageList"
            :key="item.id"
            :label="item.stname"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="选项A" v-show="!isSelectType">
        <el-input
          v-model="form.optionA"
          placeholder="请输入选项A的内容"
        ></el-input>
      </el-form-item>
      <el-form-item label="选项B" v-show="!isSelectType">
        <el-input
          v-model="form.optionB"
          placeholder="请输入选项B的内容"
        ></el-input>
      </el-form-item>
      <el-form-item label="选项C" v-show="!isSelectType">
        <el-input
          v-model="form.optionC"
          placeholder="请输入选项C的内容"
        ></el-input>
      </el-form-item>
      <el-form-item label="选项D" v-show="!isSelectType">
        <el-input
          v-model="form.optionD"
          placeholder="请输入选项D的内容"
        ></el-input>
      </el-form-item>
      <el-form-item label="选项E" v-show="!isSelectType">
        <el-input
          v-model="form.optionE"
          placeholder="请输入选项E的内容"
        ></el-input>
      </el-form-item>

      <el-form-item label="答案">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4 }"
          placeholder="请输入答案"
          v-model="form.answer"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit()">立即创建</el-button>
        <el-button @click="goback()">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "QuestionAdd",
  data() {
    return {
      isDisabled: true,
      isSelectType: true,
      form: {
        stem: "",
        questionType: {
          id: "",
          qtname: "",
        },
        difficulty: {
          id: "",
          dname: "",
        },
        subject: {
          id: "",
          sname: "",
        },
        stage: {
          id: "",
          stname: "",
        },
        optionA: "",
        optionB: "",
        optionC: "",
        optionD: "",
        optionE: "",
        answer: "",
      },
      questionTypeList: [],
      difficultyList: [],
      stageList: [],
      subjectList: [],
    };
  },
  methods: {
    goback() {
      this.$router.push("/question/list");
    },
    chooseSubject() {
      this.isDisabled = false;
      this.getStageList();
    },
    onSubmit() {
      this.axiosJSON.post("/question/add", this.form).then((result) => {
        if (result.data.code == 0) {
          this.$message({ type: "success", message: result.data.msg });
          this.$router.push("/question/list"); //编程方式跳转到学科列表组件
        } else {
          this.$message.error(result.data.msg);
        }
      });
    },
    getQTList() {
      this.axiosJSON.get("/questionType/getAll").then((result) => {
        this.questionTypeList = result.data.data;
      });
    },
    getDifList() {
      this.axiosJSON.get("/difficulty/getAll").then((result) => {
        this.difficultyList = result.data.data;
      });
    },
    getStageList() {
      this.axiosJSON
        .get("/stage/getAllWithSubjectID?subjectId=" + this.form.subject.id)
        .then((result) => {
          this.stageList = result.data.data;
        });
    },
    getSubjectList() {
      this.axiosJSON.post("/subject/getAllWithoutPage").then((result) => {
        this.subjectList = result.data.data;
      });
    },
    chooseQuestionType() {
      for (let i = 0; i < this.questionTypeList.length; i++) {
        if (this.questionTypeList[i].id == this.form.questionType.id) {
          if (this.questionTypeList[i].qtname == "选择题") {
            this.isSelectType = false;
            break;
          }
        }
        if (
          i == this.questionTypeList.length - 1 &&
          this.questionTypeList[i].qtname != "选择题"
        )
          this.isSelectType = true;
      }
    },
  },
  mounted() {
    this.$parent.questionTitle = "增加试题";
    this.getQTList(), this.getDifList(), this.getSubjectList();
  },
};
</script>

<style>
</style>
